package com.bhudy.utils;

import java.util.*;

/**
 * @Author com.bhudy
 * @Description 工具类
 * @Date 2019-10-30 23:09:37
 */
public final class XqhUtils {

    private XqhUtils() {
    }

    /**
     * 去除_，首字母转大写
     * 如：sys_user_info --> SysUserInfo
     */
    protected static String toUpperCaseFirstOneDel(String str) {
        String[] tableNames = str.split("_");
        StringBuffer sbStr = new StringBuffer();
        for (int i = 0; i < tableNames.length; i++) {
            sbStr.append(Character.toUpperCase(tableNames[i].charAt(0)));
            sbStr.append(tableNames[i].substring(1));
        }
        return sbStr.toString();
    }

    /**
     * 去除_，首字母转小写
     * 如：sys_user_info --> sysUserInfo
     */
    public static String toLowerCaseFirstOneDel(String str) {
        String[] tableNames = str.split("_");
        StringBuffer sbStr = new StringBuffer();
        sbStr.append(Character.toLowerCase(tableNames[0].charAt(0)));
        sbStr.append(tableNames[0].substring(1));
        for (int i = 1; i < tableNames.length; i++) {
            sbStr.append(Character.toUpperCase(tableNames[i].charAt(0)));
            sbStr.append(tableNames[i].substring(1));
        }
        return sbStr.toString();
    }

    /**
     * 大写字母前面加_
     * 如：SysUserInfo --> sys_user_info
     */
    public static String toUpperCaseFirstOneAdd(String str) {
        StringBuffer sbStr = new StringBuffer();
        sbStr.append(Character.toLowerCase(str.charAt(0)));
        for (int i = 1; i < str.length(); i++) {
            if (Character.isUpperCase(str.charAt(i))) {
                sbStr.append("_");
            }
            sbStr.append(Character.toLowerCase(str.charAt(i)));
        }
        return sbStr.toString();
    }

    /**
     * 格式化sql语句
     * 替换《?》 --> #{argMap.arg《》}
     *
     * @param sql sql语句
     * @param i   下标
     * @return 格式化sql的语句
     */
    public static String formatSql(String sql, Integer i, List<String> argStrList) {
        StringBuffer sqlSub = new StringBuffer(sql);
        if (sqlSub.indexOf("?") != -1) {
            sqlSub.replace(sqlSub.indexOf("?"), sqlSub.indexOf("?") + 1, argStrList.get(i));
            return formatSql(sqlSub.toString(), ++i, argStrList);
        }
        return sqlSub.toString();
    }

    public static String formatSql(String sql, Integer i) {
        StringBuffer sqlSub = new StringBuffer(sql);
        if (sqlSub.indexOf("?") != -1) {
            sqlSub.replace(sqlSub.indexOf("?"), sqlSub.indexOf("?") + 1, "#{argMap.arg" + i + "}");
            return formatSql(sqlSub.toString(), ++i);
        }
        return sqlSub.toString();
    }

    /**
     * 格式化sql语句
     * 替换多余的空格
     *
     * @param sql
     * @return
     */
    public static String trim(StringBuffer sql) {
        return trim(sql.toString());
    }

    /**
     * 格式化sql语句
     * 替换多余的空格
     *
     * @param sql
     * @return
     */
    public static String trim(String sql) {
        sql = sql.replaceAll("    ", " ");
        sql = sql.replaceAll("   ", " ");
        sql = sql.replaceAll("  ", " ");

        sql = sql.trim();
        return sql;
    }

    public static boolean isBlank(Object obj) {
        if (obj == null) return false;
        if (obj instanceof Collection) {
            return obj != null;
        } else if (obj instanceof Map) {
            return obj != null;
        } else if (obj.getClass().isArray()) {
            Object[] objs = (Object[]) obj;
            return objs != null && objs.length != 0;
        } else {
            return false;
        }
    }


    private static String createIdKey = "";
    private static Integer createIdCount = 0;

    /**
     * 生成Id
     *
     * @return
     */
    public synchronized static Long createId() {
        Date date = new Date();

        String key = String.valueOf(date.getTime());
        if (createIdKey.equals(key)) {
            createIdCount++;
        } else {
            createIdCount = 0;
        }
        createIdKey = key;

        String str = String.valueOf(createIdCount);
        while (str.length() != 5) {
            str = "0" + str;
        }

        return Long.valueOf(key + str);
    }


    private static int indexOf(String str, String str1, int i, int index) {
        if (i == 0) return str.indexOf(str1, index);
        i--;
        int s = str.indexOf(str1, index);
        if (s == -1) return -1;
        return indexOf(str, str1, i, s+1);
    }

    public static int indexOf(String str, String str1, int i){
        return indexOf(str, str1, i, 0);
    }

}
